<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addBtn(1, '')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type === 1" type="text" @click="addBtn(2, scope.row.id)">添加</el-button>
              <el-button type="text" @click="editBtn(scope.row)">编辑</el-button>
              <el-button v-if="!scope.row.children" type="text" @click="deletePermission(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 弹窗 -->
    <el-dialog :visible.sync="showDialog" :title="isEdit ? '编辑权限' : '添加权限'" @close="handleClose">
      <!-- 表单内容 -->
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="showDialog = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission, getPermissionList, delPermission, updatePermission } from '@/api/permission'
import { tranListToTreeData2 } from '@/utils/index'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      isEdit: false
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 获取权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log('list', res)

      this.list = tranListToTreeData2(res.data)
    },
    // 点击添加权限按钮
    addBtn(type, id) {
      this.showDialog = true
      this.isEdit = false
      this.formData.type = type
      this.formData.pid = id
    },
    // 点击编辑按钮编辑
    async editBtn(row) {
      this.showDialog = true
      this.isEdit = true

      this.formData = { ...row }
    },
    async submit() {
      this.isEdit ? this.doEdit() : this.doAdd()
      this.showDialog = false
      this.loadPermissionList()
    },
    // 添加权限
    async doAdd() {
      try {
        await addPermission(this.formData)
        this.$message.success('添加成功')
      } catch (error) {
        this.$message.error('添加失败')
        console.log(error)
      }
    },
    // 编辑权限
    async doEdit() {
      try {
        await updatePermission(this.formData)
        this.$message.success('编辑成功')
      } catch (error) {
        this.$message.error('编辑失败')
        console.log(error)
      }
    },
    handleClose() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    },
    // 删除权限
    async deletePermission(id) {
      // 版本一
      this.$confirm('确定删除吗，数据将无法找回', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.$message.success('删除成功')
        this.loadPermissionList()
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' })
      })

      // 版本二
      // const res = await this.$confirm('确定删除吗，数据将无法找回', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).catch(() => {
      //   this.$message({ type: 'info', message: '已取消删除' })
      // })

      // console.log(res, 'res')

      // .then(async() => {
      //   await delPermission(id)
      //   this.$message.success('删除成功')
      //   this.loadPermissionList()
      // }).catch(() => {
      //   this.$message({ type: 'info', message: '已取消删除' })
      // })

      // 版本三
      // try {
      //   await this.$confirm('确定删除吗，数据将无法找回', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   })

      //   await delPermission(id)
      //   this.$message.success('删除成功')
      //   this.loadPermissionList()
      // } catch (error) {
      //   console.log(error)
      // }
    }
  }
}
</script>
